package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.ReportTargetListDTO;
import com.xbongbong.paas.pojo.vo.ReportTargetListVO;
import com.xbongbong.paas.service.TargetLogService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * Description:
 *
 * @author 李杰
 * @version v1.0
 * @date 2019/2/19 14:55
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/reportTarget")
public class ReportTargetController {

    @Resource
    private TargetLogService targetLogService;

    /**
     * 获取简报
     *
     * @param reportTargetListDTO 入参
     * @param br
     * @return
     */
    @RequestMapping(value = "/reportList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String reportList(@RequestBody @Valid ReportTargetListDTO reportTargetListDTO, BindingResult br) {
        XbbResponse<ReportTargetListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ReportTargetListVO reportTargetListVO = targetLogService.reportList(reportTargetListDTO);
                response = new XbbResponse<>(reportTargetListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
